package com.ed.task;

import cn.hutool.json.JSONUtil;
import com.ed.domain.Option;
import com.ed.domain.Response;
import com.ed.domain.ResponseDetails;
import com.ed.dto.AnswerQDTO;
import com.ed.mapper.IResponseMapper;
import com.ed.service.IResponseDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class AnswerQuesTask {

    @Autowired
    private IResponseMapper responseMapper;
    @Autowired
    private IResponseDetailsService iResponseDetailsService;

    @Async("taskExecutor")
    @Transactional
    public void record(List<AnswerQDTO> answerQDTOS, Integer score, Integer userId,LocalDateTime startTime) {
        Response response = new Response();
        List<ResponseDetails> responseDetails = new ArrayList<>();

        response.setScore(score);
        response.setUserId(userId);
        log.info("用户id:{}", userId);
        response.setType("faq");
        response.setStartTime(startTime);
        response.setSubmitTime(LocalDateTime.now());
        responseMapper.insert(response);

        for (AnswerQDTO answerQDTO : answerQDTOS) {
            ResponseDetails responseDetail = new ResponseDetails();
            List<Option> option = answerQDTO.getOption();
            if (answerQDTO.getOptionIndex() != null) {
                Option op = option.get(answerQDTO.getOptionIndex());
                responseDetail.setScore(op.getWeight());
                responseDetail.setUserAnswer(op.getName());
            } else {
                responseDetail.setScore(0);
                responseDetail.setUserAnswer(null);
            }
            responseDetail.setResponseId(response.getId());
            responseDetail.setOption(JSONUtil.toJsonStr(option));
            responseDetail.setQuestion(answerQDTO.getQuestion());
            responseDetails.add(responseDetail);
        }
        iResponseDetailsService.saveBatch(responseDetails);
        log.info("插入完成");
    }


    @Async("taskExecutor")
    @Transactional
    public void recordHama(List<AnswerQDTO> answerQDTOS, Integer score, Integer userId,LocalDateTime startTime) {
        Response response = new Response();
        List<ResponseDetails> responseDetails = new ArrayList<>();

        response.setScore(score);
        response.setStartTime(startTime);
        response.setUserId(userId);
        response.setType("hama");
        response.setSubmitTime(LocalDateTime.now());
        responseMapper.insert(response);

        for (AnswerQDTO answerQDTO : answerQDTOS) {
            ResponseDetails responseDetail = new ResponseDetails();
            List<Option> option = answerQDTO.getOption();
            if (answerQDTO.getOptionIndex() != null) {
                Option op = option.get(answerQDTO.getOptionIndex());
                responseDetail.setScore(op.getWeight());
                responseDetail.setUserAnswer(op.getName());
            } else {
                responseDetail.setScore(0);
                responseDetail.setUserAnswer(null);
            }
            responseDetail.setResponseId(response.getId());
            responseDetail.setOption(JSONUtil.toJsonStr(option));
            responseDetail.setQuestion(answerQDTO.getQuestion());
            responseDetails.add(responseDetail);
        }
        iResponseDetailsService.saveBatch(responseDetails);
        log.info("插入完成");
    }


}
